package org.elanfox.cloud.feign.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.elanfox.cloud.feign.base.TokenProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

/**
 * @author allan
 * @date 2019-08-15
 */
@Component
@ConditionalOnBean(TokenProvider.class)
@ConditionalOnProperty(
        value = {"fox.cloud.service.enableToken"},
        havingValue = "true",
        matchIfMissing = true
)
public class TokenRequestInterceptor implements RequestInterceptor {
    @Autowired
    private TokenProvider tokenProvider;
    /**
     * 服务标识
     */
    @Value("${fox.cloud.service.id}")
    private String serviceId;
    /**
     * 版本号
     */
    @Value("${fox.cloud.service.version}")
    private String serviceVer;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        requestTemplate.header("Access-Token", tokenProvider.getToken());
        requestTemplate.header("Service-Id", serviceId);
        requestTemplate.header("Service-Version", serviceVer);
    }
}
